package com.example.main.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.mvvm.BaseViewFragment
import com.example.main.R
import com.example.main.databinding.FragmentPersonBinding

class PersonFragment : BaseViewFragment<FragmentPersonBinding>() {
    override fun initData() {
        // 初始化时读取SPUtils
        biding.tvUserName.text = com.blankj.utilcode.util.SPUtils.getInstance().getString("user_name", "张丽娜")
        biding.tvUserAddr.text = com.blankj.utilcode.util.SPUtils.getInstance().getString("user_addr", "金融科技大厦 B座")
    }

    override fun initView() {
        biding.person.setOnClickListener {
           ARouter.getInstance().build("/login/LoginActivity").navigation()
        }
        biding.ivEdit.setOnClickListener {

            val context = requireContext()
            val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_edit_person, null)
            val etName = dialogView.findViewById<android.widget.EditText>(R.id.et_name)
            val etAddr = dialogView.findViewById<android.widget.EditText>(R.id.et_addr)
            etName.setText(biding.tvUserName.text)
            etAddr.setText(biding.tvUserAddr.text)
            val dialog = android.app.AlertDialog.Builder(context)
                .setTitle("修改信息")
                .setView(dialogView)
                .setPositiveButton("保存", null)
                .setNegativeButton("取消", null)
                .create()
            dialog.setOnShowListener {
                val btn = dialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE)
                btn.setOnClickListener {
                    val name = etName.text.toString()
                    val addr = etAddr.text.toString()
                    biding.tvUserName.text = name
                    biding.tvUserAddr.text = addr
                    com.blankj.utilcode.util.SPUtils.getInstance().put("user_name", name)
                    com.blankj.utilcode.util.SPUtils.getInstance().put("user_addr", addr)
                    dialog.dismiss()
                }
            }
            dialog.show()
        }
    }

}